package com.itwn.converter;

import org.springframework.core.convert.converter.Converter;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class MyUpFileConverter implements Converter<MultipartFile,String> {
    @Override
    public String convert(MultipartFile multipartFile) {
        if (multipartFile!=null){
            String filename = multipartFile.getOriginalFilename();
            filename= UUID.randomUUID()+filename;
            //获取的服务器路径
            String serverPath="D:/nginx-1.18.0/html/upload/student/"+filename;
            try {
                //开始上传
                multipartFile.transferTo(new File(serverPath));
            } catch (IOException e) {
                e.printStackTrace();
            }
            //数据库存入的路径
            return "http://localhost/upload/student/"+filename;
        }
        return null;
    }
}
